package com.spukmk3me.animatorpack.pack0;

import com.spukmk3me.scene.Animator;
import com.spukmk3me.scene.Node;
import com.spukmk3me.util.ContinuousTimedAnimation;

public class TransformationAnimator extends Animator
{
    public TransformationAnimator()
    {
        m_animation = new ContinuousTimedAnimation();
    }
    
    public void setTransformations( TransformationData[] trans )
    {
        m_trans = trans;
    }
    
    public TransformationData[] getTransformations()
    {
        return m_trans;
    }
    
    @Override
    protected void onNodePrepareToChange( Node newNode ) {}
    
    @Override
    protected void onNodeChanged() {}

    @Override
    public void animate( int deltaTime )
    {
        if ( !isAnimating() )
            return;

        m_animation.execute( deltaTime );
        
        TransformationData trans;
        int index = m_animation.getCurrentIndex();
        
        if ( index == ContinuousTimedAnimation.LASTINDEX_TOKEN )
            trans = m_trans[ m_trans.length + 1 ];
        else
            trans = m_trans[ m_animation.getLeftIndex() ];
        
        Node node = getNode();
        
        node.setPivot( trans.px, trans.py );
        node.setTrans( trans.tx, trans.ty );
        node.setScale( trans.sx, trans.sy );
        node.setRot( trans.rangle );
    }
    
    /**
     *  Setup the animation.
     *  @param firstIndex The first frame index of animated frame sequence.
     *  @param lastFrame The last frame index of animated frame sequence.
     *  @param msPerFrame Amount of time for a frame, measured in seconds (foxed point).
     * Pass -1 value to this parameter if you want to keep the old msPerFrame.
     *  @param nFrameToStop The animation will stop after nFrameToStop frames.
     * Ignored if the mode doesn't contain MODE_FRAMESTOP.
     */
    public final void setAnimating( int mode, int firstIndex, int lastIndex, int[] times,
        int time, int limitParam )
    {
        m_animation.setAnimation( mode, firstIndex, lastIndex, times, time, limitParam );
    }

    /**
     *  Check if the sprite is animating or not.
     *  @return true if this sprite is animating, otherwise return false.
     */
    public final boolean isAnimating()
    {
        return m_animation.isAnimating();
    }
    
    private TransformationData[]        m_trans;
    private ContinuousTimedAnimation    m_animation;
}